import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;

public class GameWindow extends JFrame implements MouseListener, KeyListener, MouseMotionListener
{
  
  
  Graphics g;
  BufferedImage buffer = new BufferedImage (600,600,BufferedImage.TYPE_INT_RGB);
  int mouseX, mouseY;
  Player player;
  String currentMessage="";
  ArrayList <NPC> npcs= new ArrayList<NPC>();
  
  public void mouseMoved(MouseEvent e)
  {
    mouseX=e.getX();
    mouseY=e.getY();
  }
  
  public void mouseDragged (MouseEvent e)
  {
  }
  
  public void mousePressed(MouseEvent e)
  {
    player.changeTarget(e.getX()-15,e.getY());
  }
  
  
  public void mouseReleased(MouseEvent e)
  {
  }
  
  
  public void mouseClicked(MouseEvent e)
  {
  }
  
  
  public void mouseEntered(MouseEvent e)
  {
  }
  
  
  public void mouseExited(MouseEvent e)
  {
  }
  
  public void keyPressed(KeyEvent e)
  {
  }
  
  public void keyReleased(KeyEvent e)
  {
  }
  
  public void keyTyped(KeyEvent e)
  {
  }
  
  public void paint (Graphics g)
  {
    Graphics gi= buffer.createGraphics();
    gi.setColor(Color.white);
    gi.fillRect(0,0,600,600);
    gi.setColor(Color.red);
    gi.drawOval(player.targetX+10,player.targetY-5,10,10);
    gi.setColor (Color.black);
    gi.drawString(""+mouseX+" "+mouseY,10,36);
    gi.drawString (currentMessage, 10,48);
    gi.fillRect(player.playerX+10,player.playerY-10,20,20);
    for (NPC n:npcs)
    {
      gi.setColor(n.color);
      gi.fillRect(n.x,n.y,20,20);
      
      if (player.playerX>n.x-20&&player.playerX<n.x+20&&player.playerY>n.y-20&&player.playerY<n.y+20)
      {
        currentMessage=n.dialogue;
      }
      else if (mouseX>n.x&&mouseX<n.x+20&&mouseY>n.y&&mouseY<n.y+20)
      {
        currentMessage=n.toString();
      }
    }
    Graphics2D g2= (Graphics2D)g;
    g2.drawImage(buffer,null,0,0);
    try
    {
      Thread.sleep (10);
    }
    catch (Exception e)
    {
    }
    repaint();
  }
  
  public GameWindow()
  {
    setVisible(true);
    setSize(400,400);
    addMouseListener(this);
    addKeyListener(this);
    addMouseMotionListener(this);
    setTitle("RPG Demo");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    player= new Player();
    player.start();
    addNPCs();
  }
  
  public void addNPCs()
  {
    npcs.add(new NPC("Steve", Color.red,200,100));
    npcs.add(new NPC("Bob",Color.blue,300,200));
  }
  
  
  public static void main (String[]args)
  {
    new GameWindow();
  }
}